package com.burgeon.backend.Controller;

import com.burgeon.backend.entity.Complement;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.entity.VipAttrib;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.service.ComplementService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("complement")
public class ComplementController extends BaseController {

    @Autowired
    private ComplementService complementService;
    @Autowired
    private AccountMapper accountMapper;

    @RequestMapping("maketags")
    public ResponseResult<Void> delComplement(VipAttrib vipAttrib,String cid, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        complementService.makeUserTags(phone,vipAttrib,cid,token);

        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("tagsitem")
    public ResponseResult<List> GetTagsItem(String code,HttpSession session){
        String token = session.getAttribute("token").toString();
        ArrayList<String> data = complementService.getTagsItem(code,token);

        return new ResponseResult<List>(SUCCESS, data);
    }

    @RequestMapping("vipshow")
    public ResponseResult<List> VipShowData(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String account = accountMapper.GetAccountForToken(token);
        String phone = session.getAttribute("phone").toString();
        ArrayList data = complementService.getComplementSwitch(phone,account);

        return new ResponseResult<List>(SUCCESS, data);
    }

    @RequestMapping("getphone")
    public ResponseResult<String> getphone(HttpSession session) {
        String data = session.getAttribute("phone").toString();
        return new ResponseResult<String>(SUCCESS, data);
    }

    @RequestMapping("vipmod")
    public ResponseResult<Void> modVipData(User user, HttpSession session) {
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        System.err.println(uid+token);
        complementService.modVipData(token, user, uid);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("del")
    public ResponseResult<Void> delComplement(String id) {
        complementService.delComplement(id);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("show")
    public ResponseResult<List> ShowData(String account) {
        //String account = session.getAttribute("account").toString();
        ArrayList data = complementService.getComplement(account);

        return new ResponseResult<List>(SUCCESS, data);
    }

    @RequestMapping("create")
    public ResponseResult<Void> addComplementSwitch(Complement complement, String account) {

        String token = accountMapper.GetTokenForAccount(account);
        complement.setAccount(account);
        complement.setToken(token);
        complementService.newComplementSwitch(complement);

        return new ResponseResult<>(SUCCESS);
    }

}
